﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.SessionState;
using System.Security.Principal;
using BLL;
using Entity;
namespace Web.admin.InnerManage
{
    public partial class AddInnerTask : System.Web.UI.Page
    {
        ContestService ContestService = new ContestService();
        CompetitionService competitionService = new CompetitionService();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CompetitionList.DataSource = competitionService.getInnerCompetitions();
                CompetitionList.DataTextField = "COM_NAME";
                CompetitionList.DataValueField = "COM_ID";
                CompetitionList.DataBind();
                
                string operation = Request["OPERATION"];
                if (operation == null)
                {
                    AddContest();
                }
                string ContestId = Request["id"];
                if (ContestId == null)
                    return;

                if (operation == "Modify")
                {
                    ModifyContest(ContestId);
                }
                else if (operation == "Delete")
                {
                    DeleteContest(ContestId);
                }
            }
        }

        protected void AddContest()
        {
            IPrincipal user = HttpContext.Current.User;
            if (!(user is SitePrincipal))
                return;

            SitePrincipal sp = user as SitePrincipal;
            SiteIdentity si = sp.Identity as SiteIdentity;

            TextBoxCreator.Text = si.FullName;
        }

        protected void ModifyContest(string ContestId)
        {
            BoContests Contest = ContestService.getContest(ContestId);

            CON_NAMEBOX.Value = Contest.CON_ID;
            TextBoxName.Text = Contest.CON_NAME;

            CompetitionList.SelectedValue = Contest.CON_COMPETITIONS;
            DropDownListState.SelectedValue = Contest.CON_ISSHOW.ToString();

            TextBoxCreator.Text = Contest.CON_CREATOR;
            TextBoxStartTime.Text = Contest.CON_STARTTIME.ToString();
            TextBoxEndTime.Text = Contest.CON_ENDTIME.ToString();
            TextBoxADDRESS.Text = Contest.CON_ADDRESS;
            TextBoxTEACHERS.Text = Contest.CON_TEACHERS;
            KindEditor.Text = Contest.CON_CONTENTS;
            
        }

        //protected void AcademyList_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    TeacherList.DataSource = teacherService.GetTeachersByAcademy(AcademyList.SelectedValue);
        //    TeacherList.DataTextField = "TEA_NAME";
        //    TeacherList.DataValueField = "TEA_ID";
        //    TeacherList.DataBind();
        //}

        protected void DeleteContest(string ContestId)
        {
            BoContests contest = new BoContests();
            contest.CON_ID = ContestId;
            ContestService.DeleteContest(contest);
            Response.Redirect("InnerTask.aspx?PageCode=Task");
        }

        /// <summary>
        /// 添加事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ButtonOK_Click(object sender, EventArgs e)
        {
            //BoCompetitions compatation = new BoCompetitions();
            //compatation.COM_TYPE = 0;
            if(CON_NAMEBOX.Value=="")
            {
                BoContests Contests = new BoContests();  
                Contests.CON_NAME = TextBoxName.Text;
                Contests.CON_COMPETITIONS = CompetitionList.Text;
                Contests.CON_CREATOR = TextBoxCreator.Text;
                Contests.CON_STARTTIME = Convert.ToDateTime(TextBoxStartTime.Text);
                Contests.CON_ENDTIME = Convert.ToDateTime(TextBoxEndTime.Text);
                Contests.CON_ADDRESS = TextBoxADDRESS.Text;
                Contests.CON_TEACHERS = TextBoxTEACHERS.Text;
                Contests.CON_CONTENTS = KindEditor.Text;
                Contests.CON_ISSHOW = Convert.ToInt32(DropDownListState.SelectedValue);
                
                ContestService.CreateContest(Contests);
            }
            else
            {
                BoContests Contests = ContestService.getContest(CON_NAMEBOX.Value);
                Contests.CON_ID = CON_NAMEBOX.Value;
                Contests.CON_NAME = TextBoxName.Text;
                Contests.CON_COMPETITIONS = CompetitionList.SelectedValue;
                Contests.CON_CREATOR = TextBoxCreator.Text;
                Contests.CON_STARTTIME = Convert.ToDateTime(TextBoxStartTime.Text);
                Contests.CON_ENDTIME = Convert.ToDateTime(TextBoxEndTime.Text);
                Contests.CON_ADDRESS = TextBoxADDRESS.Text;
                Contests.CON_TEACHERS = TextBoxTEACHERS.Text;
                Contests.CON_CONTENTS = KindEditor.Text;
                Contests.CON_ISSHOW = Convert.ToInt32(DropDownListState.SelectedValue);

                ContestService.UpdateContest(Contests);
                
            }
            Response.Redirect("InnerPro.aspx?PageCode=Task");

        }
    }
}